package com.topisv.tms.service.edi;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.topisv.tms.entity.EdiGoodsSku;
import com.topisv.tms.entity.EdiOrder;
import com.topisv.tms.entity.EdiOrderItem;
import com.topisv.tms.repository.EdiGoodsSkuDao;
import com.topisv.tms.repository.EdiOrderDao;
import com.topisv.tms.repository.EdiOrderItemDao;
import com.topisv.tms.service.vo.edi.TMSSource;

//Spring Bean的标识.
@Component
// 默认将类中的所有public函数纳入事务管理.
@Transactional(readOnly = true)
public class EdiSourceService{

	private static final Logger logger = LoggerFactory.getLogger(EdiSourceService.class);
	@Autowired
	private EdiOrderDao ediOrderDao;
	@Autowired
	private EdiOrderItemDao ediOrderItemDao;
	@Autowired
	private EdiGoodsSkuDao ediGoodsSkuDao;
	
	public long findByCrtimeAdtime(Date crtime,Date adtime){
		return ediOrderDao.findByCrtimeAdtime(crtime, adtime);
	}
	
	public long findByCrtimeAdtime(Date crtime,Date adtime,String type){
		return ediOrderDao.findByCrtimeAdtime(crtime, adtime, type);
	}


	/**
	 * 根据客户编码获得商品信息
	 * @param customerCode
	 * @param ordeno
	 * @return
	 */
	public EdiOrder getEdiOrder(String customerCode,String ordeno) {
		EdiOrder ediOrder = ediOrderDao.findByOrdeno(ordeno);
		if (ediOrder == null) {
			List<EdiOrder> orders = ediOrderDao.findByCustomerCodeAndCustomerOrdeno(customerCode, ordeno);
			if(orders.isEmpty()){
				return null;
			}
			ediOrder=orders.get(0);
		}
		List<EdiOrderItem> tempEdiOrderItems = ediOrderItemDao.findByOrdeno(ordeno);
		for (EdiOrderItem tempEdiOrderItem : tempEdiOrderItems) {
			EdiGoodsSku ediGoodsSku = ediGoodsSkuDao.findByCustomerCodeAndSku(customerCode,tempEdiOrderItem.getSku());
			if (ediGoodsSku == null) {
				continue;
			}
			tempEdiOrderItem.setCol(ediGoodsSku.getCol());
			tempEdiOrderItem.setSiz(ediGoodsSku.getSiz());
		}
		ediOrder.setSkulst(tempEdiOrderItems);
		return ediOrder;
	}

	public TMSSource getEdiOrders(String customerCode,String ordenos) {
		TMSSource source = new TMSSource();
		StringBuffer ordenosBuffer = new StringBuffer("");
		StringBuffer skusBuffer = new StringBuffer("");
		String[] ordenoLists = ordenos.split(":");
		List<EdiOrder> ediOrders = new ArrayList<EdiOrder>();
		long startTimeOne = System.currentTimeMillis();
		for (String ordenoList : ordenoLists) {
			if ("".equals(ordenoList)) {
				continue;
			}
			long startTime = System.currentTimeMillis();
			EdiOrder ediOrder = ediOrderDao.findByOrdeno(ordenoList);
			if(ediOrder==null){
				List<EdiOrder> orders = ediOrderDao.findByCustomerCodeAndCustomerOrdeno(customerCode, ordenoList);
				if(!orders.isEmpty()){
					ediOrder=orders.get(0);
				}
			}
			long endTime = System.currentTimeMillis();
			logger.info("获取单个订单时间:" + (endTime - startTime));

			if (null == ediOrder) {
				if ("".equals(ordenosBuffer.toString())) {
					ordenosBuffer.append(ordenoList);
				} else {
					ordenosBuffer.append(":" + ordenoList);
				}
				continue;
			}
			startTime = System.currentTimeMillis();
			List<EdiOrderItem> tempEdiOrderItems = ediOrderItemDao.findByOrdeno(ordenoList);
			endTime = System.currentTimeMillis();
			logger.info("获取单个订单详细信息时间:" + (endTime - startTime));
			for (EdiOrderItem tempEdiOrderItem : tempEdiOrderItems) {
				startTime = System.currentTimeMillis();
				EdiGoodsSku ediGoodsSku = ediGoodsSkuDao.findByCustomerCodeAndSku(customerCode,tempEdiOrderItem.getSku());
				logger.info("获取单个商品详细信息时间:" + (endTime - startTime));
				if (ediGoodsSku == null) {
					if ("".equals(skusBuffer.toString())) {
						skusBuffer.append(ordenoList);
					} else {
						skusBuffer.append(":" + ordenoList);
					}
					continue;
				}
				tempEdiOrderItem.setCol(ediGoodsSku.getCol());
				tempEdiOrderItem.setSiz(ediGoodsSku.getSiz());
			}
			ediOrder.setSkulst(tempEdiOrderItems);
			ediOrders.add(ediOrder);
		}
		long endTime = System.currentTimeMillis();
		logger.info("for循环运行时间:" + (endTime - startTimeOne));
		source.setDeletionOrdeno(ordenosBuffer.toString());
		source.setEdiOrders(ediOrders);
		source.setDeletionSku(skusBuffer.toString());
		return source;

	}
}
